//CourseServiceImpl.java
package org.example.demo3.service.impl;

import org.example.demo3.mapper.CourseMapper;
import org.example.demo3.model.Course;
import org.example.demo3.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    CourseMapper courseMapper;

    //返回课程列表
    @Override
    public List<Course> findAll() {
        return courseMapper.selectAll();
    }

    //删除课程
    @Override
    public int remove(int id) {
        return courseMapper.deleteById(id);
    }

    //添加课程
    @Override
    public int add(Course course) {
        return courseMapper.insert(course);
    }

    //查询对应id的课程
    @Override
    public Course findOneById(int id) {
        return courseMapper.selectById(id);
    }

    //修改课程
    @Override
    public int edit(Course course) {
        return courseMapper.update(course);
    }

    //分页查询
    @Override
    public List<Course> findByPage(int currentPage, int pageSize){
        //如果页号<1 , 则从第一条记录开始，以防异常
        int formIndex = (currentPage < 1) ? 0 : (currentPage - 1) * pageSize;
        return courseMapper.selectByPage(formIndex,pageSize);
    }
}
